package com.ghca.myinfo3.crawler;

import com.ghca.myinfo3.entity.owner.Owner;
import com.ghca.myinfo3.project.entity.BidProject;
import com.ghca.myinfo3.project.repository.BidProjectRepository;
import com.ghca.myinfo3.repository.OwnerRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/17.
 */
@Service
public class SCZBBXCrawler implements Crawler {


    private Log log = LogFactory.getLog(SCZBBXCrawler.class);


    @Value("${crawler.url.sczbbx}")
    private String url;

    @Value("${crawler.timeout}")
    private int timeout;

    @Value("${crawler.enable:false}")
    private boolean enable;

    private int i;

    private List<String> lastUrls = new ArrayList<>();

    @Resource
    private OwnerRepository ownerRepository;

    @Resource
    private BidProjectRepository bidPorjectRepository;


    @Override
    @Scheduled(fixedDelay = 300000, initialDelay = 120000)
    public void doCrawl() throws IOException {
        if(!enable()) {
            log.info("爬虫功能被禁用");
            return;
        }
        log.info("开始执行周期任务，爬取网页数据。");
        Document document = Jsoup.connect(url).timeout(timeout).get();
        Element content = document.getElementsByClass("row_right_cont_down").first();
        Elements lis = content.getElementsByTag("li");
        for (int i = 0; i < lis.size(); i++) {
            String detailUrl = lis.get(i).getElementsByTag("a").first().absUrl("href");
            if (check(detailUrl)) {
                doCrawlDetail(detailUrl);
                lastUrls.add(detailUrl);
            }
        }
        if (lastUrls.size() == 100) {
            lastUrls.clear();
        }
    }


    public void doCrawlDetail(String url) throws IOException {
        BidProject project = new BidProject();
        Owner owner = new Owner();
        Document document = Jsoup.connect(url).timeout(timeout).get();
        Element details_tab = document.getElementsByClass("details_tab").first();
        project.setUrl(url);
        project.setRegion(document.getElementById("lb_region").text());
        DateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
        DateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日");
        String start = document.getElementById("lb_start").text();
        String end = document.getElementById("lb_end").text();
        if (end.endsWith("时")) {
            end += "00分";
        }
        try {
            project.setStartDate(format2.parse(start));
            project.setEndDate(format1.parse(end));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        for (Element e : details_tab.getElementsByTag("tr")) {
            Elements tds = e.getElementsByTag("td");
            String key = tds.first().text();
            if ("比选人（项目业主）".equals(key)) {
                owner.setName(tds.last().text());
            } else if ("项目名称".equals(key)) {
                project.setName(tds.last().text());
            } else if ("建设地点".equals(key)) {
                project.setAddress(tds.last().text());
            } else if ("实施时间".equals(key)) {
                project.setPeriod(tds.last().text());
            } else if ("工程规模（和/或标准）".equals(key)) {
                project.setContent(tds.last().text());
            }
        }
        Elements requirement = document.getElementsContainingOwnText("资质要求：企业资质等级不应低于（包括）：");
        project.setRequriement(requirement.text());
        Elements ownerElement = document.getElementsContainingOwnText("七、联系方式").first().nextElementSibling().getElementsByTag("strong");
        owner.setName(ownerElement.get(0).text());
        owner.setAddress(ownerElement.get(1).text());
        owner.setZipcode(ownerElement.get(2).text());
        owner.setLeader(ownerElement.get(3).text());
        owner.setPhone(ownerElement.get(4).text());
        owner.setFox(ownerElement.get(5).text());
        owner.setEmail(ownerElement.get(6).text());
        Owner findOwner = ownerRepository.findByNameAndPhone(owner.getName(), owner.getPhone());
        if (findOwner == null) {
            ownerRepository.save(owner);
            project.setOwner(owner);
        } else {
            project.setOwner(findOwner);
        }
        bidPorjectRepository.save(project);
    }

    public boolean check(String url) {
        return bidPorjectRepository.findByUrl(url) == null;
    }

    @Override
    public boolean enable() {
        return enable;
    }
}
